package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.support.DialogFastBuildUtil;
import com.qire.common.support.base.EventController;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.R;
import com.qire.manhua.activity.HomeActivity;
import com.qire.manhua.activity.helper.LoginRouter;
import com.qire.manhua.databinding.FragmentBookshelfFavoritesBinding;
import com.qire.manhua.fragment.BookshelfFavoritesFragment;
import com.qire.manhua.model.enumeration.PageTabIndex;
import com.qire.manhua.model.viewModel.impl.BookshelfFavoritesViewModel;
import com.qire.manhua.model.viewModel.impl.BookshelfFavoritesViewModel.BannerItemViewModel;
import com.qire.manhua.model.viewModel.impl.BookshelfFavoritesViewModel.FavoritesItemViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;
import androidx.databinding.library.baseAdapters.BR;

/**
 * 事件控制器
 * 首页->书架->收藏夹
 */
public class BookshelfFavoritesController extends EventController<BookshelfFavoritesFragment> implements AutoVariable {

    private BookshelfFavoritesViewModel viewModel;

    public BookshelfFavoritesController(@NonNull BookshelfFavoritesFragment owner) {
        super(owner);
    }

    @Override
    protected void bindView() {
        FragmentBookshelfFavoritesBinding viewDataBinder = owner.viewDataBinder();
        if(viewDataBinder != null) {
            viewDataBinder.setBookshelfFavoritesController(this);
        }
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.bookshelfFavoritesController, this);
    }


    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(BookshelfFavoritesViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**********************************************************************************************/

//    public void refreshLayoutOnLoadMore(RefreshLayout refreshLayout) {
//    }
    public void refreshLayoutOnRefresh(RefreshLayout refreshLayout) {
        viewModel.loadCarouselData();
        viewModel.loadFavoritesData();
    }

    public void bannerOnPageSelected(int position) {
        if(ObjectUtil.nonNull(viewModel)) {
            viewModel.setBannerIndicatorPointIndex(position);
        }
    }

    public void delBtnOnClick(View view) {

        int batchDeleteCount = viewModel.batchDeleteCount();

        if(batchDeleteCount < 1) {
            return;
        }

        DialogFastBuildUtil.builder(R.layout.dialog_common_prompt_light_l)
                .setTextView(R.id.dialog_title, "共选中" + batchDeleteCount + "个作品")
                .setTextView(R.id.dialog_content, "是否删除已选中作品?")
                .setTextView(R.id.dialog_button_light, "取消", cancelView -> DialogFastBuildUtil.dismissDialogBy(cancelView))
                .setTextView(R.id.dialog_button_dark, "确定", confirmView -> {
                    viewModel.submitDelFavorites();
                    DialogFastBuildUtil.dismissDialogBy(confirmView);
                })
                .show();

    }

    public void goSignInBtnOnClick(View view){
        AntsRouter.ROUTER.buildCard(Navigation.Page.TaskCenterPage).navigation();
    }

    public void defDataEmptyBtnOnClick(View view) {
        if(!LoginRouter.ROUTER.checkLoginRouteIfNeed()) {
            if(viewModel.isException()) {
                viewModel.loadFavoritesData();
            } else {
                HomeActivity homeActivity = ObjectUtil.as(owner.getActivity(), HomeActivity.class);
                if(ObjectUtil.nonNull(homeActivity)) {
                    homeActivity.gotoTabIndex(PageTabIndex.TAB_INDEX_HOME_RECOMMEND, PageTabIndex.TAB_INDEX_INVALID);
                }
            }
        }
    }

    public void bannerItemOnClick(View view) {
        BannerItemViewModel itemViewModel = (BannerItemViewModel) view.getTag();
        if(ObjectUtil.nonNull(itemViewModel)) {
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicReaderPage)
                    .withData("comicBookEntity", itemViewModel.toComicBookEntity())
                    .navigation();
        }
    }

    /**
     * 书架收藏书籍列表项目单击事件
     * */
    public void bookshelfCollectItemOnClick(View view){
        FavoritesItemViewModel favoriteItemViewModel = (FavoritesItemViewModel) view.getTag();
        if(viewModel.isEditing()) {
            // 在编辑状态点击事件监听
            favoriteItemViewModel.toggle();
//            fragment.noticeSelectCountChanged();
        } else {
            // 在非编辑状态点击事件监听
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicReaderPage)
                    .withData("comicBookEntity", favoriteItemViewModel.toComicBookEntity())
                    .navigation();
        }

    }

}
